const app = getApp();
var util = require('../../../utils/util.js')
// var time = this.nowTime();
Page({
  /**
   * 页面的初始数据
   */
  //   [底分, 地主123, 炸弹0+, 输赢01, 得分, 得分, 得分]
  // [10, 2, 0, 0, -20, 10, 10]
  data: {
    lastrecord: wx.getStorageSync('last-cdd') || {
      player:
        [{
          avatar: 0,
          name: '阿甲',
          score: 0
        }, {
          avatar: 0,
          name: '阿乙',
          score: 0
        }, {
          avatar: 0,
          name: '阿丙',
          score: 0
        }, {
          avatar: 0,
          name: '阿丁',
          score: 0
        }],
      time: util.formatTime(new Date()),
      winner: 0,
      panshu: 0,
      newscore: [0, 0, 0, 0],
      defen: []
    },
    openid: '',
    formId: '',
    userid: 0,
    userName: '',
    offSuan: false,
    offSet: false,
    multiArray: [
      ['赢', '1', '2', '3', '4', '5', '6', '7', '8×2', '9×2', '10×3', '11×3', '12×3', '13×4'],
      ['赢', '1', '2', '3', '4', '5', '6', '7', '8×2', '9×2', '10×3', '11×3', '12×3', '13×4'],
      ['赢', '1', '2', '3', '4', '5', '6', '7', '8×2', '9×2', '10×3', '11×3', '12×3', '13×4'],
      ['赢', '1', '2', '3', '4', '5', '6', '7', '8×2', '9×2', '10×3', '11×3', '12×3', '13×4']
    ],
    multiIndex: [0, 0, 0, 0],
    formData: {
      win: -1
    }
    //   [底分, 地主123, 炸弹0+, 输赢01, 得分, 得分, 得分]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenid();
    console.log('time = ' + this.nowTime());
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var list1 = wx.getStorageSync('last-cdd') || {
      player:
        [{
          avatar: 0,
          name: '阿甲',
          score: 0
        }, {
          avatar: 0,
          name: '阿乙',
          score: 0
        }, {
          avatar: 0,
          name: '阿丙',
          score: 0
        }, {
          avatar: 0,
          name: '阿丁',
          score: 0
        }],
      time: util.formatTime(new Date()),
      winner: 0,
      panshu: 0,
      newscore: [0, 0, 0, 0],
      defen: []
    };
    this.setData({
      lastrecord: list1
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  ///////////////////////////
  // 打开设置(关闭)
  opensetting: function (event) {
    var uid = event.currentTarget.dataset.user
    console.log("获取到的user=" + uid);
    this.setData({
      offSet: !this.data.offSet
    });
    if (uid != null) {
      this.setData({
        userid: uid,
        userName: this.data.lastrecord.player[uid].name,
        imgId: this.data.lastrecord.player[uid].avatar
      });
    }
  },
  userNameInput: function (e) {
    // console.log("name = " + e.detail.value);
    this.setData({
      userName: e.detail.value
    })
  },
  // 设置头像
  change(e) {
    console.log("head = " + e.currentTarget.dataset.id);
    this.setData({
      imgId: e.currentTarget.dataset.id
    })
  },
  // 保存设置
  saveSetting: function (event) {
    var record1 = this.data.lastrecord;
    var p1 = record1.player;
    p1[this.data.userid].name = this.data.userName;
    p1[this.data.userid].avatar = this.data.imgId;
    record1.player = p1;
    wx.setStorageSync('last-cdd', record1);
    // 最新newscore[0,0,0]
    this.setData({
      offSet: !this.data.offSet,
      lastrecord: record1
    });
  },
  MultiChange(e) {
    var sc = e.detail.value;
    console.log("获取到的:" + sc);
    var jy = false;
    var n = 0;// 校验赢家数 =1 表示正确
    var yjid = 0;
    for (var i = 0; i < sc.length; i++) {
      if (sc[i] == 0){
        n++;
        yjid = i;
      }
    }
    if (n == 1) jy = true;
    if (jy) {
      // 对
      var df = [0, 0, 0, 0];
      var zf = 0;// 算总分
      for (var i = 0; i < sc.length; i++) {
        var dq = 0;
        if (sc[i] > 0 && sc[i] < 8) dq = sc[i];
        if (sc[i] >= 8 && sc[i] < 10) dq = sc[i] * 2;
        if (sc[i] >= 10 && sc[i] < 13) dq = sc[i] * 3;
        if (sc[i] == 13) dq = sc[i] * 4;
        df[i] = -dq;
        zf += dq;
      }
      df[yjid] = zf;
      console.log("计算得分:" + df);
      var record1 = this.data.lastrecord;
      var ns = [record1.newscore[0] + df[0], record1.newscore[1] + df[1], record1.newscore[2] + df[2], record1.newscore[3] + df[3]]
      var win = 0;
      var big = ns[0];
      for (var i = 1; i < ns.length; i++) {
        if (ns[i] > big) {
          big = ns[i];
          win = i;
        }
      }
      record1.winner = win;
      // var nps = this.data.panshu + 1;
      var nps = record1.defen.length + 1;
      console.log('ps=' + nps);
      var newdf = record1.defen;
      // { difen: 10, dizhu: "3", bomb: 0, win: "0" }
      var df = {
        'id': nps,
        win: this.data.formData.win,
        time: util.formatTime(new Date()),
        'a': df[0],
        'b': df[1],
        'c': df[2],
        'd': df[3]
      };
      newdf.unshift(df);
      record1.defen = newdf;
      record1.newscore = ns;
      record1.panshu = nps;
      wx.setStorageSync('last-cdd', record1);
      // 最新newscore[0,0,0]
      this.setData({
        // offSuan: !this.data.offSuan,
        lastrecord: record1,
        multiIndex: [0, 0, 0, 0],
        formData: {
          win: -1
        }
      });
      wx.showToast({
        title: '成功',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
    } else {
      // 不对
      wx.showToast({
        title: '不对啊！',
        // icon: 'succes',
        duration: 2000,
        mask: true
      })
    }
  },
 
  // 删除
  del: function (event) {
    var record1 = this.data.lastrecord;
    var nps = record1.panshu - 1;
    var newdf = record1.defen;

    console.log(newdf);
    var aa = newdf.shift();
    var ns = [record1.newscore[0] - aa.a, record1.newscore[1] - aa.b, record1.newscore[2] - aa.c, record1.newscore[3] - aa.d];
    var win = 0;
    var big = ns[0];
    for (var i = 1; i < ns.length; i++) {
      if (ns[i] > big) {
        big = ns[i];
        win = i;
      }
    }
    record1.winner = win;
    record1.panshu = nps;
    record1.newscore = ns;
    record1.defen = newdf;
    wx.setStorageSync('last-cdd', record1);
    this.setData({
      lastrecord: record1
    });
    this.hideModal();
  },

  // 获取用户openid
  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log(res);
        console.log('云函数获取到的openid: ', res.result.openid)
        var openid = res.result.openid;
        that.setData({
          openid: openid
        })
      }
    })
  }
  ///////////////////
})